package com.tsky.casing.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.tsky.common.ActionUtil;
import com.tsky.common.BeanUtil;

/**
 * @describe: Struts2后台操作基础Action(经过登录过滤)
 * @author: cainiao
 * @version 1.0
 * @time: 2010-2-27 上午02:10:37
 */
public class BackAction extends BaseAction {

	private Map<String, String> methodMap = new HashMap<String, String>();

	protected Map<String, String> getMethodMap() {
		methodMap.put("login", "login");
		return methodMap;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		Object invokeResult = null;
		String command = this.getCommand();

		Map filters = this.getMethodMap();
		
		if (isLogin() || filters.containsKey(command)) {
			if (!BeanUtil.isEmpty(command)) {
				invokeResult = this.getClass().getDeclaredMethod(command).invoke(this);
			}

			return (invokeResult == null) ? SUCCESS : invokeResult.toString();
		} else {
			// ActionUtil.getResponse().sendRedirect("/scrum/application/login/login.jsp");
			return "login";
		}
	}

	/**
	 * 判断用户是否已经登录
	 * 
	 * @author: cainiao
	 * @time: 2010-3-10 上午06:13:50
	 * @function:
	 * @return
	 */
	public boolean isLogin() {
		Object loginUser = ActionUtil.getAttribute("loginUser");
		return !(loginUser == null);
	}

	/**
	 * 获取当前登录用户的ID
	 * 
	 * @author: cainiao
	 * @time: 2010-5-18 上午02:11:57
	 * @function:
	 * @return
	 * @throws Exception
	 */
	public String getUserID() throws Exception {
		Object loginUser = ServletActionContext.getRequest().getSession()
				.getAttribute("loginUser");
		String userID = null; 
		
		if (loginUser != null) {
			userID = (String) BeanUtil.invoke(loginUser, "getId");
		}
		
		return userID;
	}
	
	/**
	 * 
	 * @function: 获取用户名
	 * @author: cainiao
	 * @time: 2011-12-19 下午02:32:39
	 * @return
	 * @throws Exception
	 */
	public String getUserName() throws Exception {
		Object loginUser = ServletActionContext.getRequest().getSession()
				.getAttribute("loginUser");
		String userID = null; 
		
		if (loginUser != null) {
			userID = (String) BeanUtil.invoke(loginUser, "getName");
		}
		
		return userID;
	}

}
